<!-- 组件名: index -->
<!-- 创建日期: 2023/12/25 -->
<!-- 编写者: Adil -->

<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="模块名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入模块名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="info"
              plain
              icon="el-icon-back"
              size="mini"
              @click="handleBack"
            >后退</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
            >删除</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="模块名称" align="center" key="name" prop="name" v-if="columns[0].visible" :show-overflow-tooltip="true" />
          <el-table-column label="Url前缀" align="center" key="url" prop="url" v-if="columns[1].visible" :show-overflow-tooltip="true" />
          <el-table-column label="创建时间" align="center" key="createTimeStr" prop="createTimeStr" v-if="columns[2].visible" :show-overflow-tooltip="true">
            <template #default="scope">
              {{ formatTime(scope.row.createTimeStr) }}
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope" v-if="scope.row.userId !== 1">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加与编辑对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="模块昵称" prop="name">
              <el-input v-model="form.name" placeholder="请输入模块昵称" maxlength="100" />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="项目名称" prop="projectName">
              <el-input v-model="form.projectName" placeholder="请输入项目名称" />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="Url" prop="url">
              <el-input v-model="form.url" placeholder="请输入Url" />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="模块简介" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入模块简介" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import RightToolbar from "@/components/RightToolbar/index.vue";
import Pagination from "@/components/Pagination/index.vue";
import store from "@/store";
import {
  GetDeleteModel,
  GetModelListId,
  GetModuleAdd,
  GetModuleList,
  GetProjectID, GetProjectModuleUpdate,
} from "@/api/project";
import {formatTime} from "@/utils/adil";

export default {
  name: "index",
  components: {Pagination, RightToolbar},
  data() {
    return {
      // 查询参数
      queryParams: {
        id: this.$route.params.id,
        pageNum: 1,
        pageSize: 10,
        name: undefined,
      },
      // 显示搜索条件
      showSearch: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 列信息
      columns: [
        { key: 0, label: `模块名称`, visible: true },
        { key: 1, label: `Url前缀`, visible: true },
        { key: 2, label: `创建时间`, visible: true }
      ],
      // 遮罩层
      loading: false,
      // 用户表格数据
      userList: null,
      // 总条数
      total: 0,
      // 是否显示弹出层
      open: false,
      // 弹出层标题
      title: "",
      // 表单参数
      form: {
        id: this.$route.params.id,
        list_id: '',
        name: '',
        projectName: '',
        url: '',
        remark: '',
        projectId: '',
        userName: store.state.user.name
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "请输入项目名称", trigger: 'blur' }
        ],
        remark: [
          { required: true, message: "请输入项目简介", trigger: 'blur' }
        ],
        cover: [
          { required: true, message: "请上传项目封面", trigger: 'blur' }
        ]
      }
    };
  },
  // 计算属性
  computed: {},
  created() {
    this.getList()
  },
  methods: {
    formatTime,
    getList() {
      this.loading = true
      GetModuleList(this.queryParams).then(res => {
        console.log(res)
        this.userList = res.result;
        this.total = res.total;
        this.loading = false
      })
    },

    reset() {
      this.form = {
        list_id: undefined,
        name: undefined,
        projectName: undefined,
        url: undefined,
        remark: undefined,
        projectId: undefined,
        userName: store.state.user.name
      }
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.queryParams.name = undefined;
      this.handleQuery();
    },
    handleBack() {
      this.$router.back()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item._id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '新建模块'
      GetProjectID(this.$route.params.id).then(res => {
        this.form.projectName = res.result[0].name
        this.form.projectId = res.result[0]._id
        this.form.list_id = res.result[0]._id
      })
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const userId = row._id || this.ids;
      GetModelListId(userId).then(res => {
        this.form = res.result[0]
        this.open = true
        this.title = '更新模块'
      })

      console.log(row)
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row._id || this.ids
      this.$modal.confirm('是否确认删除用户编号为"' + userIds + '"的数据项？').then(function () {
        return GetDeleteModel(userIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(async valid => {
        if (valid) {
          if (this.title != '新建模块') {
            await GetProjectModuleUpdate(this.form._id, this.form)
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          } else {
            GetModuleAdd(this.form).then(res => {
              this.$modal.msgSuccess("成功");
              this.open = false
              this.getList()
            })
          }
        }
      })
    },

    /** 取消按钮 */
    cancel() {
      this.open = false;
    }
  }
}
</script>
